package com.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

/**
 * 字符编码集过滤器
 * 实现所有http请求及返回统一字符集字符集
 * @author xieyl
 */


public class EncodeFilter implements Filter
{

	private String targetEncoding;

	public void init(FilterConfig filterConfig)
	{
		targetEncoding = filterConfig.getInitParameter("encoding");
	    if(targetEncoding.equals("") || targetEncoding == null){
	    	targetEncoding = "UTF-8";
	    }	                                                                                                                
	}

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain filterChain) throws IOException, ServletException
	{
		request.setCharacterEncoding(targetEncoding);
		response.setContentType("text/html;charset=" + targetEncoding);

		filterChain.doFilter(request, response);
	}

	public void destroy()
	{
	}
}
